/**
 * @file audio_player.c
 *
 */

/*********************
*      INCLUDES
*********************/
#include "lv_watch.h"

#if USE_LV_WATCH_AUDIO_PLAYER != 0

#include <stdio.h>
#include <stdlib.h>

/*********************
*      DEFINES
*********************/
#define AUDIO_PLAYER_MAX_PAR_TAB_NUM (2) // for audio player on exercise and launcher

/**********************
*      TYPEDEFS
**********************/
typedef lv_fs_file_t ui_file_t;

/**********************
*  STATIC PROTOTYPES
**********************/
static void audio_player_prepare_destory(lv_obj_t * activity_obj);
static void audio_player_img_start_signal(lv_obj_t * img, lv_event_t e);
static void audio_player_img_pause_signal(lv_obj_t * img, lv_event_t e);
static void audio_player_img_prev_signal(lv_obj_t * img, lv_event_t e);
static void audio_player_img_next_signal(lv_obj_t * img, lv_event_t e);
static bool audio_player_display_time(void);
static NV_AUDIO_PLAYER_MODE get_audio_player_mode(void);
static void set_audio_player_mode(NV_AUDIO_PLAYER_MODE audio_player_mode);
static void get_audio_player_info(char * path, ui_file_t * pfile);
static void store_audio_player_info_to_global(char * path, char * filename, ui_file_t * pfile);
static audio_info_t * get_audio_player_info_from_filesystem(uint32_t index, bool mode);
static void audio_player_auto_play_handler(void);
static void audio_player_music_over_cb(void * para);
static void audio_player_auto_user_interface_update(void);
static void audio_player_option_btn_action(lv_obj_t * btn, lv_event_t e);
static lv_audio_player_ext_t * audio_player_get_ext(void);
static void all_songs_btn_rel_action(lv_obj_t * btn, lv_event_t e);
static void settings_btn_rel_action(lv_obj_t * btn, lv_event_t e);
static void audio_player_song_play_rel_action(lv_obj_t * btn, lv_event_t e);
static void audio_player_update_check_box(void);
static void setting_sub_btn_rel_action(lv_obj_t * btn, lv_event_t e);
static lv_obj_t * audio_player_opt_list_create(lv_obj_t * activity_obj);
static lv_obj_t * audio_player_settings_list_create(lv_obj_t * activity_obj);
static lv_obj_t * audio_player_songs_list_create(lv_obj_t * activity_obj);
static void audio_player_task(lv_task_t * task);
static void audio_player_refresh_task(lv_task_t * task);
static void audio_player_get_endtime(void);
static void volume_slider_action(lv_obj_t * slider, lv_event_t event);
static void volume_btn_action(lv_obj_t * btn, lv_event_t e);
static void progress_rate_slider_action(lv_obj_t * slider, lv_event_t event);
void audio_player_audio_ctrl_callback(AUDIO_CTRL_PRIORITY priority);
static void audio_player_songs_prev_btn_action(lv_obj_t * btn, lv_event_t e);
static void audio_player_songs_next_btn_action(lv_obj_t * btn, lv_event_t e);
static void audio_player_songs_refresh_btn_action(lv_obj_t * btn, lv_event_t e);
#if USE_LV_SD_FS
static void audio_player_sd_mounted_as_storage(lv_obj_t * obj);
static void audio_player_sd_is_full(void);
#endif

/**********************
*  STATIC VARIABLES
**********************/
enum {
    AUDIO_PLAYER_STATE_IDLE = 0,
    AUDIO_PLAYER_STATE_RUN,
    AUDIO_PLAYER_STATE_PAUSE
};
typedef uint8_t AUDIO_PLAYER_STATE;
static AUDIO_PLAYER_STATE ap_state = AUDIO_PLAYER_STATE_IDLE;
static AUDIO_PLAYER_STATE ap_store_state = AUDIO_PLAYER_STATE_IDLE;
static uint32_t     endtime = 0;
static hal_play_info_t current_playinfo;
static uint32_t     play_cnt = 0;
static uint32_t     current_view_index = 0;
static uint32_t     current_index = AUDIO_CURRENT_INDEX_INVALID;
static lv_task_t * audio_player_task_p = NULL;
static audio_info_t audio_info_global;
#if USE_LV_SD_FS
static bool sd_remount_flg = false;
static lv_task_t * inquire_sd_task_p = NULL;
#endif
static lv_obj_t * par_tab[AUDIO_PLAYER_MAX_PAR_TAB_NUM] = {};

/**********************
*      MACROS
**********************/

/**********************
 *   GLOBAL FUNCTIONS
 ***********************/
static bool audio_player_check_sdcard(lv_obj_t * par)
{
#if USE_LV_SD_FS
    if(true != sd_remount_flg) {
#if USE_LV_WATCH_SD_AUTO_MOUNT != 0
        if(true == setting_sd_mount_capture_check(SETTING_CAPTURE_SD_APP_AUDIO_PLAYER))
#else
        if(SD_MOUNTED_AS_U_STORAGE == HAL_sd_user_set(true))
#endif
        {
            printf("%s,SD card is mounted as U storage!\n", __FUNCTION__);
            audio_player_sd_mounted_as_storage(par);
            return false;
        } else {
            printf("%s,SD card is not mounted as U storage!\n", __FUNCTION__);
            sd_remount_flg = true;
            return true;
        }
    }
#endif
    return true;
}

static void audio_player_create_content(lv_obj_t * content)
{
    lv_audio_player_ext_t * img_ext = lv_obj_allocate_ext_attr(content, sizeof(lv_audio_player_ext_t));
    img_ext->label_title = lv_label_create(content, NULL);
    lv_obj_set_base_dir(img_ext->label_title, LV_BIDI_DIR_LTR);
    lv_label_set_long_mode(img_ext->label_title, LV_LABEL_LONG_SROLL);
    lv_label_set_align(img_ext->label_title, LV_LABEL_ALIGN_CENTER);
    lv_obj_add_style(img_ext->label_title, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_obj_set_width(img_ext->label_title, 180);
    if(AUDIO_CURRENT_INDEX_INVALID != current_index) {
        audio_info_t * audio_info = get_audio_player_info_from_filesystem(current_index, false);
        if(0 != strcmp(audio_info->filename,"read song error!")) {
            lv_label_set_text(img_ext->label_title, audio_info->filename);
        } else {
            if(AUDIO_PLAYER_STATE_RUN == ap_state) {
                Hal_File_Play_End();
                Hal_Audio_Manage_End_Ind(AUDIO_CTRL_PRIORITY_6);
                if(NULL != audio_player_task_p) {
                    lv_task_del(audio_player_task_p);
                    audio_player_task_p = NULL;
                }
            }
            current_index = AUDIO_CURRENT_INDEX_INVALID;
            ap_state = AUDIO_PLAYER_STATE_IDLE;
            memset(&current_playinfo, 0, sizeof(hal_play_info_t));
            endtime = 0;
            lv_label_set_text(img_ext->label_title, "");
        }
    } else {
        lv_label_set_text(img_ext->label_title, "");
    }

#ifdef USE_WATCH_LITE
    lv_obj_align(img_ext->label_title, content, LV_ALIGN_CENTER, 0, 0);
#else
    lv_obj_align(img_ext->label_title, content, LV_ALIGN_IN_TOP_MID, 0, 10);
#endif

    img_ext->label_time_s = lv_label_create(content, NULL);
    lv_obj_set_base_dir(img_ext->label_time_s, LV_BIDI_DIR_LTR);
    lv_obj_add_style(img_ext->label_time_s, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_long_mode(img_ext->label_time_s, LV_LABEL_LONG_EXPAND);

    lv_obj_t * slider = lv_slider_create(content, NULL);
    img_ext->slider = slider;
    lv_obj_set_size(slider, 210, 20);

    lv_slider_set_range(slider, 0, 10);
    lv_slider_set_value(slider, 0, LV_ANIM_OFF);

    lv_obj_set_style_local_bg_color(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, WATCH_COLOR_GRAY);
    lv_obj_set_style_local_bg_grad_color(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, LV_COLOR_SILVER);
    lv_obj_set_style_local_bg_opa(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, LV_OPA_30);
    lv_obj_set_style_local_border_color(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x40, 0x40, 0x40));
    lv_obj_set_style_local_border_opa(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, LV_OPA_30);
    lv_obj_set_style_local_radius(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
    lv_obj_set_style_local_line_color(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x20, 0x20, 0x20));
    lv_obj_set_style_local_pad_left(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, 15);
    lv_obj_set_style_local_pad_right(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, 15);
    lv_obj_set_style_local_pad_top(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, 15);
    lv_obj_set_style_local_pad_bottom(slider, LV_SLIDER_PART_BG, LV_STATE_DEFAULT, 15);

    lv_color_t slide_color = WATCH_COLOR_BLUE;

    lv_obj_set_style_local_bg_color(slider, LV_SLIDER_PART_INDIC, LV_STATE_DEFAULT, slide_color);
    lv_obj_set_style_local_bg_grad_color(slider, LV_SLIDER_PART_INDIC, LV_STATE_DEFAULT, slide_color);
    lv_obj_set_style_local_border_width(slider, LV_SLIDER_PART_INDIC, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_shadow_width(slider, LV_SLIDER_PART_INDIC, LV_STATE_DEFAULT, 0);

    lv_obj_set_style_local_radius(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
    lv_obj_set_style_local_bg_color(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, slide_color);
    lv_obj_set_style_local_bg_grad_color(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, slide_color);
    lv_obj_set_style_local_shadow_color(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_obj_set_style_local_shadow_width(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, 5);
    lv_obj_set_style_local_border_width(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, 2);
    lv_obj_set_style_local_border_opa(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, LV_OPA_50);
    lv_obj_set_style_local_pad_top(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_bottom(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_left(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_right(slider, LV_SLIDER_PART_KNOB, LV_STATE_DEFAULT, 0);

    lv_obj_set_click(slider, false);
    lv_obj_set_event_cb(slider, progress_rate_slider_action);

    lv_obj_t * btn = lv_btn_create(content, NULL);
    lv_obj_add_style(btn, LV_BTN_PART_MAIN, &lv_style_transp_fit);
    lv_btn_set_fit2(btn, LV_FIT_NONE, LV_FIT_TIGHT);
    lv_btn_set_layout(btn, LV_LAYOUT_CENTER);
    lv_obj_set_width(btn, LV_HOR_RES / 4);
    lv_obj_set_event_cb(btn, audio_player_option_btn_action);
    lv_obj_add_protect(btn, LV_PROTECT_PRESS_LOST);

    lv_watch_obj_add_element(btn);

    lv_obj_t * label_option = lv_label_create(btn, img_ext->label_title);
    lv_label_set_text_id(label_option, WATCH_TEXT_ID_OPT);
    lv_obj_add_style(label_option, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_align(label_option, LV_LABEL_ALIGN_CENTER);
    if(lv_obj_get_base_dir(content) == LV_BIDI_DIR_RTL)
        lv_obj_align(btn, content, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
    else {
#ifdef USE_WATCH_LITE
        lv_obj_align(btn, content, LV_ALIGN_IN_TOP_LEFT, 0, 0);
#else 
        lv_obj_align(btn, content, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);
#endif
    }

    img_ext->img_btn_start_pause = lv_img_create(content, NULL);
    LV_ASSERT_MEM(img_ext->img_btn_start_pause);
    if(img_ext->img_btn_start_pause == NULL) {
        return;
    }
    lv_img_set_auto_size(img_ext->img_btn_start_pause, true);
    lv_obj_set_click(img_ext->img_btn_start_pause, true);
    lv_watch_obj_add_element(img_ext->img_btn_start_pause);

    if(AUDIO_PLAYER_STATE_RUN == ap_state) {
        lv_img_set_src(img_ext->img_btn_start_pause, ICON_AUDIO_PLAYER_PAUSE);
        lv_obj_set_event_cb(img_ext->img_btn_start_pause, audio_player_img_pause_signal);
        lv_obj_align(img_ext->img_btn_start_pause, content, LV_ALIGN_IN_BOTTOM_MID, 0, -LV_HOR_RES / 8);
    } else {
        lv_img_set_src(img_ext->img_btn_start_pause, ICON_AUDIO_PLAYER_PLAY);
        lv_obj_set_event_cb(img_ext->img_btn_start_pause, audio_player_img_start_signal);
        lv_obj_align(img_ext->img_btn_start_pause, content, LV_ALIGN_IN_BOTTOM_MID, 0, -LV_HOR_RES / 8);
    }

    audio_player_get_endtime();
    audio_player_auto_user_interface_update();
    lv_obj_align(img_ext->label_time_s, content, LV_ALIGN_IN_TOP_MID, 0, lv_obj_get_height(img_ext->label_title) + 10);
    lv_obj_align(slider, img_ext->label_time_s, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);

    img_ext->img_btn_next = lv_img_create(content, img_ext->img_btn_start_pause);
    lv_img_set_auto_size(img_ext->img_btn_next, true);
    lv_obj_set_click(img_ext->img_btn_next, true);
    lv_img_set_src(img_ext->img_btn_next, ICON_AUDIO_PLAYER_NEXT);
    if(lv_obj_get_base_dir(content) == LV_BIDI_DIR_RTL)
        lv_obj_set_event_cb(img_ext->img_btn_next, audio_player_img_prev_signal);
    else
        lv_obj_set_event_cb(img_ext->img_btn_next, audio_player_img_next_signal);
    lv_obj_align(img_ext->img_btn_next, img_ext->img_btn_start_pause, LV_ALIGN_OUT_RIGHT_MID, 0, 0);
    lv_watch_obj_add_element(img_ext->img_btn_next);

    img_ext->img_btn_prev = lv_img_create(content, img_ext->img_btn_start_pause);
    lv_img_set_auto_size(img_ext->img_btn_prev, true);
    lv_obj_set_click(img_ext->img_btn_prev, true);
    lv_img_set_src(img_ext->img_btn_prev, ICON_AUDIO_PLAYER_PREV);
    if(lv_obj_get_base_dir(content) == LV_BIDI_DIR_RTL)
        lv_obj_set_event_cb(img_ext->img_btn_prev, audio_player_img_next_signal);
    else
        lv_obj_set_event_cb(img_ext->img_btn_prev, audio_player_img_prev_signal);
    lv_obj_align(img_ext->img_btn_prev, img_ext->img_btn_start_pause, LV_ALIGN_OUT_LEFT_MID, 0, 0);
    lv_watch_obj_add_element(img_ext->img_btn_prev);

    btn = lv_btn_create(content, NULL);
    lv_obj_add_style(btn, LV_BTN_PART_MAIN, &lv_style_transp_fit);
    lv_btn_set_fit2(btn, LV_FIT_NONE, LV_FIT_TIGHT);
    lv_btn_set_layout(btn, LV_LAYOUT_CENTER);
    lv_obj_set_width(btn, LV_HOR_RES / 4);
    lv_obj_set_event_cb(btn, volume_btn_action);
    lv_obj_add_protect(btn, LV_PROTECT_PRESS_LOST);

    lv_watch_obj_add_element(btn);

    label_option = lv_label_create(btn, img_ext->label_title);
    lv_label_set_text_id(label_option, WATCH_TEXT_ID_VOLUME);
    lv_obj_add_style(label_option, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_align(label_option, LV_LABEL_ALIGN_CENTER);
    if(lv_obj_get_base_dir(content) == LV_BIDI_DIR_RTL)
        lv_obj_align(btn, content, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);
        else {
#ifdef USE_WATCH_LITE
        lv_obj_align(btn, content, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
#else 
        lv_obj_align(btn, content, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
#endif
    }
}

void audio_player_create_tab(lv_obj_t * par)
{
    printf("%s: par %p\n", __FUNCTION__, par);

    if(par_tab[AUDIO_PLAYER_MAX_PAR_TAB_NUM - 1]) {
        printf("%s: warning, too many audio players\n", __FUNCTION__);
        return;
    }

    if(par_tab[0] && (1 < AUDIO_PLAYER_MAX_PAR_TAB_NUM)) {
        for(int8_t i = AUDIO_PLAYER_MAX_PAR_TAB_NUM - 2;i >= 0;i--) {
            par_tab[i + 1] = par_tab[i];
        }
    }
    par_tab[0] = par;

#if USE_LV_SD_FS
    if(SD_REMOUNT_OK > HAL_sd_user_get()) {
        audio_player_sd_mounted_as_storage(par);
        return;
    }
#endif

    audio_player_create_content(par);

    if((AUDIO_PLAYER_STATE_RUN == ap_state) && (NULL == audio_player_task_p)) {
        audio_player_task_p = lv_task_create(audio_player_task, 1000, LV_TASK_PRIO_MID, NULL);
    }
}

void audio_player_destroy_tab(lv_obj_t * tab)
{
    printf("%s\n", __FUNCTION__);

    if(tab != par_tab[0]) {
        printf("%s: warning, wrong tab %p", __FUNCTION__, tab);
        for(uint8_t i = 0;i < AUDIO_PLAYER_MAX_PAR_TAB_NUM;i++) {
            printf(", %p", par_tab[i]);
        }
        printf("\n");
        return;
    }

    for(uint8_t i = 0;i < (AUDIO_PLAYER_MAX_PAR_TAB_NUM - 1);i++) {
        par_tab[i] = par_tab[i + 1];
    }
    par_tab[AUDIO_PLAYER_MAX_PAR_TAB_NUM - 1] = NULL;

    if(NULL == par_tab[0]) {
#if USE_LV_SD_FS
        if(inquire_sd_task_p) {
            lv_task_del(inquire_sd_task_p);
            inquire_sd_task_p = NULL;
        }
#endif

#if USE_LV_WATCH_AUDIO_PLAYER == 0
        if(AUDIO_PLAYER_STATE_RUN == ap_state) {
            audio_player_img_pause_signal(NULL, LV_EVENT_RELEASED);
        }
#endif

        lv_watch_png_cache_all_free();
    } else {
        lv_obj_clean(par_tab[0]);
        bool is_sd_mounted = false;
#if USE_LV_SD_FS
        if(SD_REMOUNT_OK > HAL_sd_user_get()) {
            audio_player_sd_mounted_as_storage(par_tab[0]);
            is_sd_mounted = true;
        }
#endif
        if(false == is_sd_mounted) {
            audio_player_create_content(par_tab[0]);
        }
    }

    if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_ALL_SONGS)) {
        lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_ALL_SONGS));
    }
    if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_OPT)) {
        lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_OPT));
    }
    if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_SETTINGS)) {
        lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_SETTINGS));
    }
}

lv_obj_t * audio_player_create(lv_obj_t * activity_obj)
{
    lv_obj_t * obj;
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_AUDIO_PLAYER;
        activity_ext.create = audio_player_create;
        activity_ext.prepare_destory = audio_player_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) {
            return(NULL);
        }
    }
    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) {
        return(NULL);
    }
    lv_watch_obj_set_anim_mode(obj, LV_WATCH_ANIM_HOR_RIGHT_HIDE);

    if(!audio_player_check_sdcard(obj)) return obj;

    lv_obj_t * content = lv_cont_create(obj, NULL);
    LV_ASSERT_MEM(content);
    if(content == NULL) return NULL;
    lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_size(content,  LV_HOR_RES, LV_VER_RES);
    lv_cont_set_layout(content, LV_LAYOUT_OFF);

    lv_obj_t * img = lv_img_create(content, NULL);
    lv_img_set_src(img, ICON_AUDIO_PLAYER_BACKGROUND);
    lv_obj_set_size(img, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_click(content, false);
    lv_obj_set_click(img, false);

    audio_player_create_content(content);

    if((AUDIO_PLAYER_STATE_RUN == ap_state) && (NULL == audio_player_task_p)) {
        audio_player_task_p = lv_task_create(audio_player_task, 1000, LV_TASK_PRIO_MID, NULL);
    }

    return(obj);
} /* audio_player_create */

static void progress_rate_slider_action(lv_obj_t * slider, lv_event_t e)
{
    if(e != LV_EVENT_CLICKED) return;
    if(lv_slider_is_dragged(slider)) return;

    uint16_t progress_rate = lv_slider_get_value(slider);
    lv_audio_player_ext_t * ext = audio_player_get_ext();

    if((NULL == ext)) {
        return;
    }

    current_playinfo.curPlayTimeMs = (int32_t)(progress_rate * 1000);
    current_playinfo.PlayProgress = (int32_t)((double)current_playinfo.curPlayTimeMs / endtime * 10000.f);

    printf("%s:progress_rate is %u,curPlayTimeMs is %d\n",__FUNCTION__,progress_rate,current_playinfo.curPlayTimeMs);

    if(AUDIO_PLAYER_STATE_RUN == ap_state) {
        Hal_File_Audio_Seekto(current_playinfo.curPlayTimeMs);
    }
    audio_player_auto_user_interface_update();

    return;
}

static void volume_slider_action(lv_obj_t * slider, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
    if(lv_slider_is_dragged(slider)) return;

    uint16_t volume = lv_slider_get_value(slider);

    setting_set_ring_volume((uint8_t)volume, true, false);

    printf("%s:volume is %d\n", __FUNCTION__, volume);

    Hal_Audio_SetVolume(volume);

    return;
}

static void volume_btn_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) return;
    slider_create(NULL, SETTING_VOLUME, 90, volume_slider_action);
    return;
}

static void audio_player_refresh_task(lv_task_t * task)
{
    if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_ALL_SONGS)) {
        lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_ALL_SONGS));
    }

    if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_OPT)) {
        lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_OPT));
    }
}

void audio_player_audio_ctrl_callback(AUDIO_CTRL_PRIORITY priority)
{
    printf("%s,priority is %d\n", __FUNCTION__, priority);
    if(AUDIO_CTRL_PRIORITY_6 == priority) {
        if(AUDIO_PLAYER_STATE_RUN == ap_store_state) {
            watch_wakeup_lcd(true);
            audio_info_t * audio_info = get_audio_player_info_from_filesystem(current_index, false);
            char * path = (char *)lv_mem_alloc(strlen(audio_info->path) + strlen(audio_info->filename) + 1);
            memset(path, 0, strlen(audio_info->path) + strlen(audio_info->filename) + 1);
            strcpy(path, audio_info->path);
            strcat(path, audio_info->filename);
            audio_player_get_endtime();
            Hal_File_Play_Onetime(path,
                                  query_current_volume(),
                                  audio_player_music_over_cb,
                                  NULL,
                                  current_playinfo.PlayProgress);
            lv_mem_free(path);

            ap_state = AUDIO_PLAYER_STATE_RUN;
            printf("%s\n", __FUNCTION__);
            watch_set_sleep_lcd_only(true);
            audio_player_auto_user_interface_update();
            if(audio_player_get_ext()) {
                if(NULL == audio_player_task_p) {
                    audio_player_task_p = lv_task_create(audio_player_task, 1000, LV_TASK_PRIO_MID, NULL);
                }
            }
            Hal_Audio_Manage_Start_Req(AUDIO_CTRL_PRIORITY_6, audio_player_audio_ctrl_callback);
        }
    } else if(AUDIO_CTRL_PRIORITY_6 > priority) {
        if(AUDIO_PLAYER_STATE_RUN == ap_state) {
            ap_store_state = ap_state;
            ap_state = AUDIO_PLAYER_STATE_PAUSE;
            watch_set_sleep_lcd_only(false);
            audio_player_auto_user_interface_update();
            Hal_Audio_Get_PlayInformation(&current_playinfo);
            printf("%s:curPlayTimeMs is %d,PlayProgress is %d\n", __FUNCTION__, current_playinfo.curPlayTimeMs, current_playinfo.PlayProgress);
            Hal_File_Play_End();
            if(NULL != audio_player_task_p) {
                lv_task_del(audio_player_task_p);
                audio_player_task_p = NULL;
            }
        }
    }
}

static void audio_player_song_play_rel_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) return;

#if USE_LV_SD_FS
    if(true != sd_remount_flg) {
#if USE_LV_WATCH_SD_AUTO_MOUNT != 0
        if(true == setting_sd_mount_capture_check(SETTING_CAPTURE_SD_APP_AUDIO_PLAYER)) {
#else
        if(SD_MOUNTED_AS_U_STORAGE == HAL_sd_user_set(true)) {
#endif
            printf("%s,SD card is mounted as U storage!\n", __FUNCTION__);
            return;
        } else {
            sd_remount_flg = true;
        }
    }
#endif

    char * path;
    lv_task_t * audio_player_task_once = NULL;
    lv_obj_user_data_t * user_data = lv_obj_get_user_data_ptr(btn);

    current_index = user_data->user_num;
    audio_info_t * audio_info = get_audio_player_info_from_filesystem(current_index, false);

    if(AUDIO_PLAYER_STATE_RUN == ap_state) {
        Hal_File_Play_End();
    }
    memset(&current_playinfo, 0, sizeof(hal_play_info_t));
    ap_state = AUDIO_PLAYER_STATE_RUN;

    audio_player_task_once = lv_task_create(audio_player_refresh_task, 100, LV_TASK_PRIO_HIGH, NULL);
    lv_task_once(audio_player_task_once);
    lv_task_ready(audio_player_task_once);

    path = (char *)lv_mem_alloc(strlen(audio_info->path) + strlen(audio_info->filename) + 1);
    memset(path, 0, strlen(audio_info->path) + strlen(audio_info->filename) + 1);
    strcpy(path, audio_info->path);
    strcat(path, audio_info->filename);
    printf("%s,path:%s\n", __FUNCTION__, path);
    audio_player_get_endtime();
    Hal_Audio_Manage_Start_Req(AUDIO_CTRL_PRIORITY_6, audio_player_audio_ctrl_callback);
    Hal_File_Play_Onetime(path,
                          query_current_volume(),
                          audio_player_music_over_cb,
                          NULL,
                          0);

    lv_mem_free(path);
    watch_set_sleep_lcd_only(true);
    audio_player_auto_user_interface_update();
    if(NULL == audio_player_task_p) {
        audio_player_task_p = lv_task_create(audio_player_task, 1000, LV_TASK_PRIO_MID, NULL);
    }

    return;
}

static void audio_player_songs_prev_btn_action(lv_obj_t * btn, lv_event_t e)
{
    (void)btn;
    if(LV_EVENT_CLICKED != e) return;

    if(play_cnt <= AUDIO_PAGE_MAX_NUM) {
        return;
    }

    audio_info_t * audio_info = NULL;
    uint32_t  count = 0;
    lv_audio_player_ext_t * ext = audio_player_get_ext();

    printf("%s,play_cnt:%d,current_view_index:%d,current_index:%d,ext->count:%d\n", __FUNCTION__, play_cnt, current_view_index, current_index, ext->count);
    if(0 == current_view_index) {
        if(play_cnt % AUDIO_PAGE_MAX_NUM) {
            current_view_index = play_cnt - play_cnt % AUDIO_PAGE_MAX_NUM;
            count = play_cnt % AUDIO_PAGE_MAX_NUM;
        } else {
            current_view_index = play_cnt - AUDIO_PAGE_MAX_NUM;
            count = AUDIO_PAGE_MAX_NUM;
        }
    } else {
        current_view_index -= AUDIO_PAGE_MAX_NUM;
        count = AUDIO_PAGE_MAX_NUM;
    }

    lv_obj_t * label = NULL;
    lv_obj_user_data_t * user_data = NULL;
    lv_list_focus_btn(lv_obj_get_parent(lv_obj_get_parent(ext->btn_sub[0])), NULL);
    for(uint32_t i = 0; i < ext->count; i++) {
        lv_obj_set_event_cb(ext->btn_sub[i], NULL);
        user_data = lv_obj_get_user_data_ptr(ext->btn_sub[i]);
        user_data->user_num = 0;
        label = lv_obj_get_child(ext->btn_sub[i], NULL);
        lv_label_set_text(label, "");
        lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL);
        lv_obj_set_width(label, 220);
    }
    ext->count = count;
    for(uint32_t i = 0; i < count; i++) {
        audio_info = get_audio_player_info_from_filesystem(current_view_index + i, false);
        if(NULL == audio_info) {
            printf("%s,audio_info is NULL!\n", __FUNCTION__);
        }
        lv_obj_set_event_cb(ext->btn_sub[i], audio_player_song_play_rel_action);
        user_data = lv_obj_get_user_data_ptr(ext->btn_sub[i]);
        user_data->user_num = current_view_index + i;
        label = lv_obj_get_child(ext->btn_sub[i], NULL);
        lv_label_set_text(label, audio_info->filename);
        lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL);
        lv_obj_set_width(label, 220);
        if(current_view_index + i == current_index) {
            lv_list_focus_btn(lv_obj_get_parent(lv_obj_get_parent(ext->btn_sub[i])), ext->btn_sub[i]);
        }
    }

    return;
}

static void audio_player_songs_refresh_btn_action(lv_obj_t * btn, lv_event_t e)
{
    (void)btn;
    if(LV_EVENT_CLICKED != e) return;
    if(AUDIO_PLAYER_STATE_RUN == ap_state) {
        audio_player_img_pause_signal(NULL, LV_EVENT_RELEASED);
    }
    audio_info_t * audio_info = get_audio_player_info_from_filesystem(0, true);
    uint32_t  count = 0;

    if(NULL == audio_info) {
        printf("%s,audio_info is NULL!\n", __FUNCTION__);
    }

    audio_info = NULL;

    if((current_index >= play_cnt)
            && (AUDIO_CURRENT_INDEX_INVALID != current_index)) {
        current_index = 0;
    }

    lv_audio_player_ext_t * ext = audio_player_get_ext();

    if(0 == play_cnt) {
        printf("%s,play_cnt is 0!\n", __FUNCTION__);
        lv_label_set_text(ext->label_title, "");
    }

    printf("%s,play_cnt:%d,current_view_index:%d,current_index:%d,ext->count:%d\n", __FUNCTION__, play_cnt, current_view_index, current_index, ext->count);
    if(AUDIO_CURRENT_INDEX_INVALID != current_index) {
        current_view_index = current_index - current_index % AUDIO_PAGE_MAX_NUM;
    } else {
        current_view_index = 0;
    }

    if(play_cnt % AUDIO_PAGE_MAX_NUM) {
        if(play_cnt - current_view_index < AUDIO_PAGE_MAX_NUM) {
            count = play_cnt - current_view_index;
        } else {
            count = AUDIO_PAGE_MAX_NUM;
        }
    } else {
        if(play_cnt) {
            count = AUDIO_PAGE_MAX_NUM;
        } else {
            count = 0;
        }
    }

    lv_obj_t * label = NULL;
    lv_obj_user_data_t * user_data = NULL;
    lv_list_focus_btn(lv_obj_get_parent(lv_obj_get_parent(ext->btn_sub[0])), NULL);
    for(uint32_t i = 0; i < ext->count; i++) {
        lv_obj_set_event_cb(ext->btn_sub[i], NULL);
        user_data = lv_obj_get_user_data_ptr(ext->btn_sub[i]);
        user_data->user_num = 0;
        label = lv_obj_get_child(ext->btn_sub[i], NULL);
        lv_label_set_text(label, "");
        lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL);
        lv_obj_set_width(label, 220);

    }
    ext->count = count;
    for(uint32_t i = 0; i < count; i++) {
        audio_info = get_audio_player_info_from_filesystem(current_view_index + i, false);
        if(NULL == audio_info) {
            printf("%s,audio_info is NULL!\n", __FUNCTION__);
        }
        lv_obj_set_event_cb(ext->btn_sub[i], audio_player_song_play_rel_action);
        user_data = lv_obj_get_user_data_ptr(ext->btn_sub[i]);
        user_data->user_num = current_view_index + i;
        label = lv_obj_get_child(ext->btn_sub[i], NULL);
        lv_label_set_text(label, audio_info->filename);
        lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL);
        lv_obj_set_width(label, 220);
        if(current_view_index + i == current_index) {
            lv_list_focus_btn(lv_obj_get_parent(lv_obj_get_parent(ext->btn_sub[i])), ext->btn_sub[i]);
        }
    }

    return;
}

static void audio_player_songs_next_btn_action(lv_obj_t * btn, lv_event_t e)
{
    (void)btn;
    if(LV_EVENT_CLICKED != e) return;

    if(play_cnt <= AUDIO_PAGE_MAX_NUM) {
        return;
    }

    audio_info_t * audio_info = NULL;
    uint32_t  count = 0;
    lv_audio_player_ext_t * ext = audio_player_get_ext();

    printf("%s,play_cnt:%d,current_view_index:%d,current_index:%d,ext->count:%d\n", __FUNCTION__, play_cnt, current_view_index, current_index, ext->count);
    if(play_cnt % AUDIO_PAGE_MAX_NUM) {
        if((play_cnt - play_cnt % AUDIO_PAGE_MAX_NUM) == current_view_index) {
            current_view_index = 0;
        } else {
            current_view_index += AUDIO_PAGE_MAX_NUM;
        }

        if(play_cnt - current_view_index < AUDIO_PAGE_MAX_NUM) {
            count = play_cnt - current_view_index;
        } else {
            count = AUDIO_PAGE_MAX_NUM;
        }
    } else {
        if((play_cnt - AUDIO_PAGE_MAX_NUM) == current_view_index) {
            current_view_index = 0;
        } else {
            current_view_index += AUDIO_PAGE_MAX_NUM;
        }
        count = AUDIO_PAGE_MAX_NUM;
    }

    lv_obj_t * label = NULL;
    lv_obj_user_data_t * user_data = NULL;
    lv_list_focus_btn(lv_obj_get_parent(lv_obj_get_parent(ext->btn_sub[0])), NULL);
    for(uint32_t i = 0; i < ext->count; i++) {
        lv_obj_set_event_cb(ext->btn_sub[i], NULL);
        user_data = lv_obj_get_user_data_ptr(ext->btn_sub[i]);
        user_data->user_num = 0;
        label = lv_obj_get_child(ext->btn_sub[i], NULL);
        lv_label_set_text(label, "");
        lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL);
        lv_obj_set_width(label, 220);
        //lv_obj_set_click(ext->btn_sub[i], false);
    }
    ext->count = count;
    for(uint32_t i = 0; i < count; i++) {
        audio_info = get_audio_player_info_from_filesystem(current_view_index + i, false);
        if(NULL == audio_info) {
            printf("%s,audio_info is NULL!\n", __FUNCTION__);
        }
        lv_obj_set_event_cb(ext->btn_sub[i], audio_player_song_play_rel_action);
        user_data = lv_obj_get_user_data_ptr(ext->btn_sub[i]);
        user_data->user_num = current_view_index + i;
        label = lv_obj_get_child(ext->btn_sub[i], NULL);
        lv_label_set_text(label, audio_info->filename);
        lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL);
        lv_obj_set_width(label, 220);
        if(current_view_index + i == current_index) {
            lv_list_focus_btn(lv_obj_get_parent(lv_obj_get_parent(ext->btn_sub[i])), ext->btn_sub[i]);
        }
    }

    return;
}

static void all_songs_btn_rel_action(lv_obj_t * btn, lv_event_t e)
{
    (void)btn;
    if(LV_EVENT_CLICKED != e) return;
    audio_info_t * audio_info = NULL;

    uint32_t  count = 0;
    lv_audio_player_ext_t * ext = audio_player_get_ext();

    lv_obj_t * obj = audio_player_songs_list_create(NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return;

    if(play_cnt) {
        audio_info = get_audio_player_info_from_filesystem(0, false);
    } else {
        audio_info = get_audio_player_info_from_filesystem(0, true);
    }
    if(AUDIO_CURRENT_INDEX_INVALID != current_index) {
        current_view_index = current_index - current_index % AUDIO_PAGE_MAX_NUM;
    } else {
        current_view_index = 0;
    }


    if(play_cnt % AUDIO_PAGE_MAX_NUM) {
        if(play_cnt - current_view_index < AUDIO_PAGE_MAX_NUM) {
            count = play_cnt - current_view_index;
        } else {
            count = AUDIO_PAGE_MAX_NUM;
        }
    } else {
        if(play_cnt) {
            count = AUDIO_PAGE_MAX_NUM;
        } else {
            count = 0;
        }
    }

    ext->count = count;

    lv_obj_t * label = NULL;
    lv_obj_t * btn_sub   = NULL;
    lv_obj_user_data_t * user_data = NULL;
    for(uint32_t i = 0; i < count; i++) {
        audio_info = get_audio_player_info_from_filesystem(current_view_index + i, false);
        if(NULL == audio_info) {
            printf("%s,audio_info is NULL!\n", __FUNCTION__);
        }
        btn_sub = lv_watch_list_add(obj, NULL, WATCH_TEXT_ID_ALL_SONGS, audio_player_song_play_rel_action);
        ext->btn_sub[i] = btn_sub;
        user_data = lv_obj_get_user_data_ptr(btn_sub);
        user_data->user_num = current_view_index + i;
        label = lv_obj_get_child(btn_sub, NULL);
        if(lv_obj_get_base_dir(btn_sub) == LV_BIDI_DIR_RTL) {
            lv_obj_set_base_dir(label, LV_BIDI_DIR_LTR);
            lv_label_set_align(label, LV_LABEL_ALIGN_RIGHT);
        }
        lv_label_set_text(label, audio_info->filename);
        lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL);
        lv_obj_set_width(label, 220);
        if(current_view_index + i == current_index) {
            lv_list_focus_btn(lv_obj_get_parent(lv_obj_get_parent(ext->btn_sub[i])), ext->btn_sub[i]);
        }
    }

    for(uint32_t i = count; i < AUDIO_PAGE_MAX_NUM; i++) {
        btn_sub = lv_watch_list_add(obj, NULL, WATCH_TEXT_ID_ALL_SONGS, NULL);
        ext->btn_sub[i] = btn_sub;
        user_data = lv_obj_get_user_data_ptr(btn_sub);
        user_data->user_num = 0;
        label = lv_obj_get_child(btn_sub, NULL);
        lv_label_set_text(label, "");
        lv_label_set_long_mode(label, LV_LABEL_LONG_SROLL);
        lv_obj_set_width(label, 220);
    }

    return;
}

static void setting_sub_btn_rel_action(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED != e) return;
    NV_AUDIO_PLAYER_MODE audio_player_mode;

    lv_obj_user_data_t * user_data = lv_obj_get_user_data_ptr(btn);

    audio_player_mode = (NV_AUDIO_PLAYER_MODE)user_data->user_num;

    set_audio_player_mode(audio_player_mode);
    audio_player_update_check_box();

    return;
}

static void settings_btn_rel_action(lv_obj_t * btn, lv_event_t e)
{
    (void)btn;
    if(LV_EVENT_CLICKED != e) return;
    lv_audio_player_ext_t * ext = audio_player_get_ext();

    lv_obj_t * obj = audio_player_settings_list_create(NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return;

    lv_obj_user_data_t * user_data = NULL;

    lv_watch_list_set_name_txt(obj, WATCH_TEXT_ID_SETTINGS);
    ext->img_order = lv_watch_list_add(obj, (void *)ICON_AUDIO_PLAYER_UNCHECKBOX, WATCH_TEXT_ID_ORDER_PLAY, setting_sub_btn_rel_action);
    user_data = lv_obj_get_user_data_ptr(ext->img_order);
    user_data->user_num = 0;
    ext->img_shuffle = lv_watch_list_add(obj, (void *)ICON_AUDIO_PLAYER_UNCHECKBOX, WATCH_TEXT_ID_SHUFFLE_PLAY, setting_sub_btn_rel_action);
    user_data = lv_obj_get_user_data_ptr(ext->img_shuffle);
    user_data->user_num = 1;
    ext->img_all_repeat = lv_watch_list_add(obj, (void *)ICON_AUDIO_PLAYER_UNCHECKBOX, WATCH_TEXT_ID_ALL_REPEAT_PLAY, setting_sub_btn_rel_action);
    user_data = lv_obj_get_user_data_ptr(ext->img_all_repeat);
    user_data->user_num = 2;
    ext->img_repeat_once = lv_watch_list_add(obj, (void *)ICON_AUDIO_PLAYER_UNCHECKBOX, WATCH_TEXT_ID_REPEAT_ONCE_PLAY, setting_sub_btn_rel_action);
    user_data = lv_obj_get_user_data_ptr(ext->img_repeat_once);
    user_data->user_num = 3;
    audio_player_update_check_box();

    return;
}

static void audio_player_option_btn_action(lv_obj_t * btn, lv_event_t e)
{
    (void)btn;

    if(LV_EVENT_CLICKED != e) return;

    lv_obj_t * obj = audio_player_opt_list_create(NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return;

    lv_watch_list_set_name_txt(obj, WATCH_TEXT_ID_OPT);

    lv_watch_list_add(obj, (void *)ICON_SETTING_LANGUAGE, WATCH_TEXT_ID_ALL_SONGS, all_songs_btn_rel_action);
    lv_watch_list_add(obj, (void *)ICON_SETTING_LANGUAGE, WATCH_TEXT_ID_SETTINGS, settings_btn_rel_action);

    return;
}

void audio_player_create_event_cb(lv_obj_t * btn, lv_event_t e)
{
    (void)btn;
    if(LV_EVENT_CLICKED == e) {
        lv_watch_png_cache_all_free();

        lv_obj_t * obj = audio_player_create(NULL);
        LV_ASSERT_MEM(obj);
    }
}

#if USE_LV_BLUETOOTH != 0
void watch_bt_avrcp_connect_message_handle(void * msg)
{
    mmi_msg_bt_avrcp_key_t * res_msg = (mmi_msg_bt_avrcp_key_t *)msg;
    printf("bt+++ watch_bt_avrcp_connect_message_handle key_state = %d\n", res_msg->key_state);
    if(MMI_BT_AVRCP_KEY_CNF == res_msg->header.MsgId) {
        if(1 == res_msg->key_state) {
            printf("bt avrcp key released ksycode = %2x \n", res_msg->key);
        } else {
            printf("bt avrcp key pressed ksycode = %2x \n", res_msg->key);
            if(voip_connect_check_req()) {
                voip_ring_off_req();
                return;
            }
            switch(res_msg->key) {
                case AVRCP_KEY_PLAY:
                    if(AUDIO_PLAYER_STATE_PAUSE == ap_state) {
                        audio_player_img_start_signal(NULL, LV_EVENT_RELEASED);
                    }
                    break;

                case AVRCP_KEY_STOP:
                case AVRCP_KEY_PAUSE:
                    if(AUDIO_PLAYER_STATE_RUN == ap_state) {
                        audio_player_img_pause_signal(NULL, LV_EVENT_RELEASED);
                    }
                    break;

                case AVRCP_KEY_FORWARD:
                    audio_player_img_next_signal(NULL, LV_EVENT_RELEASED);
                    break;

                case AVRCP_KEY_BACKWARD:
                    audio_player_img_prev_signal(NULL, LV_EVENT_RELEASED);
                    break;
                default:
                    break;
            }
        }
    }
}

void audio_player_handle_bt_handset_connect_cnf(uint8_t connect)
{
    if(connect) {
        // connected
        if(AUDIO_PLAYER_STATE_RUN == ap_state) {
            audio_player_img_pause_signal(NULL, LV_EVENT_RELEASED);
            audio_player_img_start_signal(NULL, LV_EVENT_RELEASED);
        }
    } else {
        // disconnected
        if(AUDIO_PLAYER_STATE_RUN == ap_state) {
            audio_player_img_pause_signal(NULL, LV_EVENT_RELEASED);
        }
    }
}
#endif

/**********************
*   STATIC FUNCTIONS
**********************/
static void audio_player_prepare_destory(lv_obj_t * activity_obj)
{
#if 0
    audio_player_info_clean();

    endtime = 0;
#endif
    if(NULL != audio_player_task_p) {
        lv_task_del(audio_player_task_p);
        audio_player_task_p = NULL;
    }
#if 0
    if(AUDIO_PLAYER_STATE_RUN == ap_state) {
        ap_state = AUDIO_PLAYER_STATE_PAUSE;
        Hal_Audio_Get_PlayInformation(&current_playinfo);
        printf("%s:curPlayTimeMs is %d,PlayProgress is %d\n", __FUNCTION__, current_playinfo.curPlayTimeMs, current_playinfo.PlayProgress);
        Hal_File_Play_End();
        Hal_Audio_Manage_End_Ind(AUDIO_CTRL_PRIORITY_6);
    }
#endif
    if(AUDIO_PLAYER_STATE_RUN != ap_state) {
#if USE_LV_SD_FS
        if(true == sd_remount_flg) {
            printf("%s,SD card is released by user!\n", __FUNCTION__);
#if USE_LV_WATCH_SD_AUTO_MOUNT != 0
            setting_sd_mount_app_release(SETTING_CAPTURE_SD_APP_AUDIO_PLAYER);
#else
            if(HAL_sd_user_get()) {
                HAL_sd_user_set(false);
            }
#endif
            sd_remount_flg = false;
        }
#endif

        watch_set_sleep_lcd_only(false);
    }

    lv_watch_png_cache_all_free();
}
#if 0
void audio_player_info_clean(void)
{
    audio_player_info_t  * pnode = NULL;
    for(uint32_t i = 0; i < play_cnt; i++) {
        pnode = find_the_node(i);
        lv_mem_free(pnode->path);
        lv_mem_free(pnode->filename);
    }
    if(NULL != audio_player_info) {
        _lv_ll_clear(audio_player_info);
        lv_mem_free(audio_player_info);
        audio_player_info = NULL;
        play_cnt = 0;
    }
    printf("clean all audio list in audio_player_info_clean!\n");
}
#endif

static void audio_player_list_prepare_destory(lv_obj_t * activity_obj)
{
    current_view_index = 0;
    lv_audio_player_ext_t * ext = audio_player_get_ext();
    for(uint32_t i = 0; i < AUDIO_PAGE_MAX_NUM; i++) {
        ext->btn_sub[i] = NULL;
    }
    ext->count = 0;
    lv_watch_png_cache_all_free();
}

static lv_obj_t * audio_player_opt_list_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_AUDIO_PLAYER_OPT;
        activity_ext.create = audio_player_opt_list_create;
        activity_ext.prepare_destory = audio_player_list_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_watch_obj_set_anim_mode(obj, LV_WATCH_ANIM_HOR_RIGHT_HIDE);

    lv_watch_obj_ext_t * obj_ext = lv_obj_get_ext_attr(obj);
    if(obj_ext == NULL) return NULL;

    /*Allocate the tab type specific extended data*/
    lv_watch_list_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_watch_list_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;
    /*ext realloc based on watch_obj_ext, do not memeset it*/
    //memset(ext, 0, sizeof(lv_watch_list_ext_t));

    /*Initialize the allocated 'ext' */
    ext->name = NULL;
    ext->list = NULL;
    ext->list_name_hide = 0;

    /* content */
    lv_obj_t * content = lv_cont_create(obj, NULL);
    lv_obj_set_size(content, LV_HOR_RES, LV_VER_RES);
    lv_cont_set_layout(content, LV_LAYOUT_COLUMN_MID);
    LV_ASSERT_MEM(content);
    if(content == NULL) return NULL;
    lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_click(content, false);
    lv_obj_align(content, obj, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t * label_cont = lv_cont_create(content, NULL);
    lv_obj_set_size(label_cont, lv_obj_get_width(content), 30);
    lv_obj_add_style(label_cont, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
    lv_cont_set_layout(label_cont, LV_LAYOUT_OFF);
    lv_cont_set_fit2(label_cont, LV_FIT_TIGHT, LV_FIT_TIGHT);
    lv_obj_set_click(label_cont, false);

    lv_obj_t * label = lv_label_create(label_cont, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_label_set_text(label, "");
    lv_obj_set_click(label, false);
    ext->name = label_cont;

    lv_obj_t * list = lv_list_create(content, NULL);
    LV_ASSERT_MEM(list);
    if(list == NULL) return NULL;
    ext->list = list;

    lv_watch_obj_add_element(lv_page_get_scrl(list));

    /* update list style*/
    lv_obj_add_style(list, LV_LIST_PART_BG, &lv_watch_style_transp);

    uint8_t pad_ver = 4;
    lv_obj_add_style(list, LV_LIST_PART_SCROLLABLE, &lv_watch_style_transp);
    lv_obj_set_style_local_pad_top(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, pad_ver);
    lv_obj_set_style_local_pad_bottom(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, pad_ver);

    lv_list_set_scrollbar_mode(list, LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_width(list, lv_obj_get_width(content));
    lv_coord_t height = lv_obj_get_height(content)
                        - lv_obj_get_height(label_cont)
                        - pad_ver * 2;
    lv_obj_set_height(list, height);
    lv_watch_obj_add_element(list);

    return obj;
}

static lv_obj_t * audio_player_settings_list_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_AUDIO_PLAYER_SETTINGS;
        activity_ext.create = audio_player_settings_list_create;
        activity_ext.prepare_destory = audio_player_list_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_watch_obj_set_anim_mode(obj, LV_WATCH_ANIM_HOR_RIGHT_HIDE);

    lv_watch_obj_ext_t * obj_ext = lv_obj_get_ext_attr(obj);
    if(obj_ext == NULL) return NULL;

    /*Allocate the tab type specific extended data*/
    lv_watch_list_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_watch_list_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;
    /*ext realloc based on watch_obj_ext, do not memeset it*/
    //memset(ext, 0, sizeof(lv_watch_list_ext_t));

    /*Initialize the allocated 'ext' */
    ext->name = NULL;
    ext->list = NULL;
    ext->list_name_hide = 0;

    /* content */
    lv_obj_t * content = lv_cont_create(obj, NULL);
    lv_obj_set_size(content, lv_obj_get_width(obj), lv_obj_get_height(obj));
    lv_cont_set_layout(content, LV_LAYOUT_COLUMN_MID);
    LV_ASSERT_MEM(content);
    if(content == NULL) return NULL;
    lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_click(content, false);
    lv_obj_align(content, obj, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t * label_cont = lv_cont_create(content, NULL);
    lv_obj_set_size(label_cont, lv_obj_get_width(content), 30);
    lv_obj_add_style(label_cont, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
    lv_cont_set_layout(label_cont, LV_LAYOUT_OFF);
    lv_cont_set_fit2(label_cont, LV_FIT_TIGHT, LV_FIT_TIGHT);
    lv_obj_set_click(label_cont, false);

    lv_obj_t * label = lv_label_create(label_cont, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_label_set_text(label, "");
    lv_obj_set_click(label, false);
    ext->name = label_cont;

    lv_obj_t * list = lv_list_create(content, NULL);
    LV_ASSERT_MEM(list);
    if(list == NULL) return NULL;
    ext->list = list;

    lv_watch_obj_add_element(lv_page_get_scrl(list));

    /* update list style*/
    lv_obj_add_style(list, LV_LIST_PART_BG, &lv_watch_style_transp);

    uint8_t pad_ver = 4;
    lv_obj_add_style(list, LV_LIST_PART_SCROLLABLE, &lv_watch_style_transp);
    lv_obj_set_style_local_pad_top(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, pad_ver);
    lv_obj_set_style_local_pad_bottom(list, LV_LIST_PART_SCROLLABLE, LV_STATE_DEFAULT, pad_ver);

    lv_list_set_scrollbar_mode(list, LV_SCROLLBAR_MODE_ON);
    lv_obj_set_width(list, lv_obj_get_width(content));
    lv_coord_t height = lv_obj_get_height(content)
                        - lv_obj_get_height(label_cont)
                        - pad_ver * 2;
    lv_obj_set_height(list, height);
    lv_watch_obj_add_element(list);

    return obj;
}

static lv_obj_t * audio_player_songs_list_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_AUDIO_PLAYER_ALL_SONGS;
        activity_ext.create = audio_player_songs_list_create;
        activity_ext.prepare_destory = audio_player_list_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_watch_obj_set_anim_mode(obj, LV_WATCH_ANIM_HOR_RIGHT_HIDE);

    lv_watch_obj_ext_t * obj_ext = lv_obj_get_ext_attr(obj);
    if(obj_ext == NULL) return NULL;

    /*Allocate the tab type specific extended data*/
    lv_watch_list_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_watch_list_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;
    /*ext realloc based on watch_obj_ext, do not memeset it*/
    //memset(ext, 0, sizeof(lv_watch_list_ext_t));

    /*Initialize the allocated 'ext' */
    ext->name = NULL;
    ext->list = NULL;
    ext->list_name_hide = 0;

    /* content */
    lv_obj_t * content = lv_cont_create(obj, NULL);
    lv_obj_set_size(content, lv_obj_get_width(obj), lv_obj_get_height(obj) - 36);
    LV_ASSERT_MEM(content);
    if(content == NULL) return NULL;
    lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
    lv_obj_set_click(content, false);
    lv_obj_align(content, obj, LV_ALIGN_IN_TOP_LEFT, 0, 0);

    lv_obj_t * label_cont = lv_cont_create(content, NULL);
    lv_obj_set_size(label_cont, lv_obj_get_width(content), 20);
    lv_obj_add_style(label_cont, LV_OBJ_PART_MAIN, &lv_watch_style_transp);
    lv_cont_set_layout(label_cont, LV_LAYOUT_COLUMN_MID);
    lv_cont_set_fit2(label_cont, LV_FIT_TIGHT, LV_FIT_TIGHT);
    lv_obj_align(label_cont, content, LV_ALIGN_IN_TOP_MID, -5, 0);
    lv_obj_set_click(label_cont, false);

    lv_obj_t * label = lv_label_create(label_cont, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_obj_set_size(label, lv_obj_get_width(obj), 20);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_label_set_text_id(label, WATCH_TEXT_ID_ALL_SONGS);
    lv_obj_set_click(label, false);
    ext->name = label_cont;

    lv_obj_t * list = lv_list_create(content, NULL);
    LV_ASSERT_MEM(list);
    if(list == NULL) return NULL;
    ext->list = list;

    lv_cont_set_fit2(list, LV_FIT_PARENT, LV_FIT_TIGHT);
    lv_watch_obj_add_element(lv_page_get_scrl(list));

    /* update list style*/

    uint8_t pad_ver = 1;
    lv_obj_add_style(list, LV_LIST_PART_BG, &lv_watch_style_transp);
    lv_obj_set_style_local_pad_left(list, LV_LIST_PART_BG, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_top(list, LV_LIST_PART_BG, LV_STATE_DEFAULT, pad_ver - 1);
    lv_obj_set_style_local_pad_bottom(list, LV_LIST_PART_BG, LV_STATE_DEFAULT, pad_ver);

    lv_list_set_scrollbar_mode(list, LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_width(list, lv_obj_get_width(content));
    lv_obj_align(list, label_cont, LV_ALIGN_OUT_BOTTOM_MID, -7, 0);
    lv_watch_obj_add_element(list);

    content = lv_cont_create(obj, NULL);
    LV_ASSERT_MEM(content);
    if(content == NULL) return NULL;
    lv_obj_set_size(content, lv_obj_get_width(obj), 36);
    lv_cont_set_layout(content, LV_LAYOUT_ROW_MID);
    lv_obj_set_style_local_bg_opa(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
    lv_obj_set_style_local_pad_left(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_set_style_local_pad_inner(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
    lv_obj_set_click(content, false);
    lv_obj_align(content, obj, LV_ALIGN_IN_BOTTOM_MID, 0, 0);

    lv_obj_t * btn = lv_btn_create(content, NULL);
    lv_obj_add_style(btn, LV_BTN_PART_MAIN, &lv_style_transp_fit);
    lv_btn_set_layout(btn, LV_LAYOUT_CENTER);
    lv_obj_set_width(btn, LV_HOR_RES * 3 / 10);
    lv_obj_set_height(btn, 36);
    lv_obj_set_event_cb(btn, audio_player_songs_prev_btn_action);
    lv_obj_add_protect(btn, LV_PROTECT_PRESS_LOST);

    lv_watch_obj_add_element(btn);

    lv_obj_t * label_btn = lv_label_create(btn, NULL);
    lv_label_set_text_id(label_btn, WATCH_TEXT_ID_PREV);
    lv_obj_add_style(label_btn, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_align(label_btn, LV_LABEL_ALIGN_CENTER);

    btn = lv_btn_create(content, NULL);
    lv_obj_add_style(btn, LV_BTN_PART_MAIN, &lv_style_transp_fit);
    lv_btn_set_layout(btn, LV_LAYOUT_CENTER);
    lv_obj_set_width(btn, LV_HOR_RES * 3 / 10);
    lv_obj_set_height(btn, 36);
    lv_obj_set_event_cb(btn, audio_player_songs_refresh_btn_action);
    lv_obj_add_protect(btn, LV_PROTECT_PRESS_LOST);

    lv_watch_obj_add_element(btn);

    label_btn = lv_label_create(btn, NULL);
    lv_label_set_text_id(label_btn, WATCH_TEXT_ID_REFRESH_LIST);
    lv_obj_add_style(label_btn, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_align(label_btn, LV_LABEL_ALIGN_CENTER);

    btn = lv_btn_create(content, NULL);
    lv_obj_add_style(btn, LV_BTN_PART_MAIN, &lv_style_transp_fit);
    lv_btn_set_layout(btn, LV_LAYOUT_CENTER);
    lv_obj_set_width(btn, LV_HOR_RES * 3 / 10);
    lv_obj_set_height(btn, 36);
    lv_obj_set_event_cb(btn, audio_player_songs_next_btn_action);
    lv_obj_add_protect(btn, LV_PROTECT_PRESS_LOST);

    lv_watch_obj_add_element(btn);

    label_btn = lv_label_create(btn, NULL);
    lv_label_set_text_id(label_btn, WATCH_TEXT_ID_NEXT);
    lv_obj_add_style(label_btn, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_align(label_btn, LV_LABEL_ALIGN_CENTER);

    return obj;
}

static void audio_player_update_check_box(void)
{
    lv_audio_player_ext_t * ext = audio_player_get_ext();

    if(NULL == ext) {
        return;
    }

    lv_img_set_src(lv_obj_get_child_back(ext->img_order, NULL), ICON_AUDIO_PLAYER_UNCHECKBOX);
    lv_img_set_src(lv_obj_get_child_back(ext->img_shuffle, NULL), ICON_AUDIO_PLAYER_UNCHECKBOX);
    lv_img_set_src(lv_obj_get_child_back(ext->img_all_repeat, NULL), ICON_AUDIO_PLAYER_UNCHECKBOX);
    lv_img_set_src(lv_obj_get_child_back(ext->img_repeat_once, NULL), ICON_AUDIO_PLAYER_UNCHECKBOX);

    switch(get_audio_player_mode()) {
        case NV_AUDIO_ORDER_PLAY:
            lv_img_set_src(lv_obj_get_child_back(ext->img_order, NULL), ICON_AUDIO_PLAYER_CHECKBOX);
            break;
        case NV_AUDIO_SHUFFLE_PLAY:
            lv_img_set_src(lv_obj_get_child_back(ext->img_shuffle, NULL), ICON_AUDIO_PLAYER_CHECKBOX);
            break;
        case NV_AUDIO_ALL_REPEAT_PLAY:
            lv_img_set_src(lv_obj_get_child_back(ext->img_all_repeat, NULL), ICON_AUDIO_PLAYER_CHECKBOX);
            break;
        case NV_AUDIO_REPEAT_ONCE_PLAY:
            lv_img_set_src(lv_obj_get_child_back(ext->img_repeat_once, NULL), ICON_AUDIO_PLAYER_CHECKBOX);
            break;
        default:
            break;
    }
}

static lv_audio_player_ext_t * audio_player_get_ext(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER);
    if(activity_obj != NULL) {
        lv_obj_t * watch_obj;
        lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
        if(watch_obj == NULL) return NULL;

        if(watch_obj) {
            lv_audio_player_ext_t * ext = lv_obj_get_ext_attr(lv_obj_get_child(watch_obj, NULL));
            return ext;
        }
    } else {
        if(par_tab[0]) {
            lv_audio_player_ext_t * ext = lv_obj_get_ext_attr(par_tab[0]);
            return ext;
        }
    }
    return NULL;
}

static void audio_player_auto_user_interface_update(void)
{
    lv_audio_player_ext_t * img_ext = audio_player_get_ext();
    audio_info_t * audio_info = NULL;

    if(NULL != img_ext) {
        audio_info = get_audio_player_info_from_filesystem(current_index, false);
    } else {
        printf("img_ext is NULL in audio_player_auto_user_interface_update\n");
        return;
    }

    printf("%s,current_index is %u\n", __FUNCTION__, current_index);
    if(0 != strcmp(audio_info->filename,"read song error!")) {
        lv_label_set_text(img_ext->label_title, audio_info->filename);
    }

    audio_player_display_time();

    if(AUDIO_PLAYER_STATE_RUN == ap_state) {
        lv_img_set_src(img_ext->img_btn_start_pause, ICON_AUDIO_PLAYER_PAUSE);
        lv_obj_set_event_cb(img_ext->img_btn_start_pause, audio_player_img_pause_signal);
    } else {
        lv_img_set_src(img_ext->img_btn_start_pause, ICON_AUDIO_PLAYER_PLAY);
        lv_obj_set_event_cb(img_ext->img_btn_start_pause, audio_player_img_start_signal);
    }
}

#if 0
static audio_player_info_t * find_the_node(uint32_t index)
{
    audio_player_info_t  * pnode = NULL;
    uint32_t i = 0;

    if((AUDIO_CURRENT_INDEX_INVALID == index)) {
        return NULL;
    }

    for(i = 0; i < play_cnt; i++) {
        if(i) {
            pnode = lv_ll_get_next(audio_player_info, pnode);
        } else {
            pnode = lv_ll_get_head(audio_player_info);
        }
        if(pnode->index == index) {
            break;
        }
    }

    return pnode;
}
#endif

static void audio_player_autoplay_task(lv_task_t * t)
{
    audio_player_auto_play_handler();
    audio_player_auto_user_interface_update();
}

static void audio_player_music_over_cb(void * para)
{
    printf("%s\n", __FUNCTION__);
    Hal_File_Play_End();
    Hal_Audio_Manage_End_Ind(AUDIO_CTRL_PRIORITY_6);
    if(AUDIO_PLAYER_STATE_RUN == ap_state) {
        lv_task_t * audio_player_task_once = lv_task_create(audio_player_autoplay_task, 200, LV_TASK_PRIO_MID, NULL);
        lv_task_once(audio_player_task_once);
    }
}

static void audio_player_auto_play_handler(void)
{
    printf("%s\n", __FUNCTION__);
    char * path = NULL;

    audio_info_t * audio_info = NULL;

    audio_info = get_audio_player_info_from_filesystem(current_index, false);

    if(0 == play_cnt) {
        printf("play_cnt is 0 in audio_player_auto_play_handler\n");
        current_index = AUDIO_CURRENT_INDEX_INVALID;
        ap_state = AUDIO_PLAYER_STATE_IDLE;
        if(NULL != audio_player_task_p) {
            lv_task_del(audio_player_task_p);
            audio_player_task_p = NULL;
        }
        return;
    }

    memset(&current_playinfo, 0, sizeof(hal_play_info_t));

    switch(get_audio_player_mode()) {
        case NV_AUDIO_ORDER_PLAY:
            if((current_index + 1) == play_cnt) {
                ap_state = AUDIO_PLAYER_STATE_PAUSE;
                watch_set_sleep_lcd_only(false);
            } else {
                audio_info = get_audio_player_info_from_filesystem(++current_index, false);
                path = (char *)lv_mem_alloc(strlen(audio_info->path) + strlen(audio_info->filename) + 1);
                memset(path, 0, strlen(audio_info->path) + strlen(audio_info->filename) + 1);
                strcpy(path, audio_info->path);
                strcat(path, audio_info->filename);
                audio_player_get_endtime();
                if(AUDIO_CTRL_PRIORITY_6 == Hal_Audio_Manage_Start_Req(AUDIO_CTRL_PRIORITY_6, audio_player_audio_ctrl_callback)) {
                    Hal_File_Play_Onetime(path,
                                          query_current_volume(),
                                          audio_player_music_over_cb,
                                          NULL,
                                          0);
                } else {
                    ap_store_state = ap_state;
                    ap_state = AUDIO_PLAYER_STATE_PAUSE;
                    memset(&current_playinfo, 0, sizeof(hal_play_info_t));
                }
                lv_mem_free(path);
            }
            break;

        case NV_AUDIO_SHUFFLE_PLAY:
            current_index = rand() % (play_cnt);
            audio_player_get_endtime();
            audio_info = get_audio_player_info_from_filesystem(current_index, false);
            path = (char *)lv_mem_alloc(strlen(audio_info->path) + strlen(audio_info->filename) + 1);
            memset(path, 0, strlen(audio_info->path) + strlen(audio_info->filename) + 1);
            strcpy(path, audio_info->path);
            strcat(path, audio_info->filename);
            if(AUDIO_CTRL_PRIORITY_6 == Hal_Audio_Manage_Start_Req(AUDIO_CTRL_PRIORITY_6, audio_player_audio_ctrl_callback)) {
                Hal_File_Play_Onetime(path,
                                      query_current_volume(),
                                      audio_player_music_over_cb,
                                      NULL,
                                      0);
            } else {
                ap_store_state = ap_state;
                ap_state = AUDIO_PLAYER_STATE_PAUSE;
                memset(&current_playinfo, 0, sizeof(hal_play_info_t));
            }
            lv_mem_free(path);
            break;

        case NV_AUDIO_ALL_REPEAT_PLAY:
            if((current_index + 1) == play_cnt) {
                audio_info = get_audio_player_info_from_filesystem(0, false);
                path = (char *)lv_mem_alloc(strlen(audio_info->path) + strlen(audio_info->filename) + 1);
                memset(path, 0, strlen(audio_info->path) + strlen(audio_info->filename) + 1);
                strcpy(path, audio_info->path);
                strcat(path, audio_info->filename);
                current_index = 0;
                audio_player_get_endtime();
                if(AUDIO_CTRL_PRIORITY_6 == Hal_Audio_Manage_Start_Req(AUDIO_CTRL_PRIORITY_6, audio_player_audio_ctrl_callback)) {
                    Hal_File_Play_Onetime(path,
                                          query_current_volume(),
                                          audio_player_music_over_cb,
                                          NULL,
                                          0);
                } else {
                    ap_store_state = ap_state;
                    ap_state = AUDIO_PLAYER_STATE_PAUSE;
                    memset(&current_playinfo, 0, sizeof(hal_play_info_t));
                }
            } else {
                audio_info = get_audio_player_info_from_filesystem(++current_index, false);
                path = (char *)lv_mem_alloc(strlen(audio_info->path) + strlen(audio_info->filename) + 1);
                memset(path, 0, strlen(audio_info->path) + strlen(audio_info->filename) + 1);
                strcpy(path, audio_info->path);
                strcat(path, audio_info->filename);
                audio_player_get_endtime();
                if(AUDIO_CTRL_PRIORITY_6 == Hal_Audio_Manage_Start_Req(AUDIO_CTRL_PRIORITY_6, audio_player_audio_ctrl_callback)) {
                    Hal_File_Play_Onetime(path,
                                          query_current_volume(),
                                          audio_player_music_over_cb,
                                          NULL,
                                          0);
                } else {
                    ap_store_state = ap_state;
                    ap_state = AUDIO_PLAYER_STATE_PAUSE;
                    memset(&current_playinfo, 0, sizeof(hal_play_info_t));
                }
            }
            lv_mem_free(path);
            break;

        case NV_AUDIO_REPEAT_ONCE_PLAY:
            path = (char *)lv_mem_alloc(strlen(audio_info->path) + strlen(audio_info->filename) + 1);
            memset(path, 0, strlen(audio_info->path) + strlen(audio_info->filename) + 1);
            strcpy(path, audio_info->path);
            strcat(path, audio_info->filename);
            audio_player_get_endtime();
            if(AUDIO_CTRL_PRIORITY_6 == Hal_Audio_Manage_Start_Req(AUDIO_CTRL_PRIORITY_6, audio_player_audio_ctrl_callback)) {
                Hal_File_Play_Onetime(path,
                                      query_current_volume(),
                                      audio_player_music_over_cb,
                                      NULL,
                                      0);
            } else {
                ap_store_state = ap_state;
                ap_state = AUDIO_PLAYER_STATE_PAUSE;
                memset(&current_playinfo, 0, sizeof(hal_play_info_t));
            }
            lv_mem_free(path);
            break;

        default:
            break;
    }
}

static NV_AUDIO_PLAYER_MODE get_audio_player_mode(void)
{
    uint32_t length = sizeof(nv_watch_audio_player_t);
    nv_watch_audio_player_t * nvm = (nv_watch_audio_player_t *)lv_mem_alloc(length);
    NV_AUDIO_PLAYER_MODE audio_player_mode = 0;
    if(length != UI_NV_Read_Req(NV_SECTION_UI_AUDIO_PLAYER, 0, length, (uint8_t *)nvm)) {
        printf("read audio_player_mode from nvm error\n");
        lv_mem_free(nvm);
        return 0;
    }
    audio_player_mode = nvm->audio_player_mode;
    lv_mem_free(nvm);

    return audio_player_mode;
}

static void set_audio_player_mode(NV_AUDIO_PLAYER_MODE audio_player_mode)
{
    nv_watch_audio_player_t * nvm = (nv_watch_audio_player_t *)lv_mem_alloc(sizeof(nv_watch_audio_player_t));
    memset(nvm, 0, sizeof(nv_watch_audio_player_t));
    nvm->audio_player_mode = audio_player_mode;
    UI_NV_Write_Req(NV_SECTION_UI_AUDIO_PLAYER, 0, sizeof(nv_watch_audio_player_t), (uint8_t *)nvm);
    lv_mem_free(nvm);
}

static void store_audio_player_info_to_global(char * path, char * filename, ui_file_t * pfile)
{
    printf("%s,path:%s,filename:%s\n", __FUNCTION__, path, filename);
    char path_temp[AUDIO_PATH_MAX];
    char filename_temp[AUDIO_PATH_MAX];
    uint32_t ret_len = 0;

    if((strlen(filename) + strlen(path) - 1) > AUDIO_PATH_MAX) {
        printf("filename and path are too long,can not be saved!\n");
        return;
    }
    memset(path_temp, 0, AUDIO_PATH_MAX);
    memset(filename_temp, 0, AUDIO_PATH_MAX);
    memcpy(path_temp, path, (strlen(path) - 1)); //without *
    memcpy(filename_temp, filename, strlen(filename));
    play_cnt++;
    lv_fs_seek(pfile, 0);
    lv_fs_write(pfile, &play_cnt, sizeof(uint32_t), &ret_len);
    if(0 == ret_len) {
        printf("write play_cnt in sdfs is wrong!\n");
        return;
    }
    lv_fs_seek(pfile, (play_cnt - 1) * AUDIO_PATH_MAX * 2 + sizeof(uint32_t));
    lv_fs_write(pfile, path_temp, AUDIO_PATH_MAX, &ret_len);
    if(0 == ret_len) {
        printf("write path in sdfs is wrong!\n");
        return;
    }
    lv_fs_seek(pfile, (play_cnt - 1) * AUDIO_PATH_MAX * 2 + AUDIO_PATH_MAX + sizeof(uint32_t));
    lv_fs_write(pfile, filename_temp, AUDIO_PATH_MAX, &ret_len);
    if(0 == ret_len) {
        printf("write filename in sdfs is wrong!\n");
        return;
    }

    printf("play_cnt:%u\n", play_cnt);
}

static audio_info_t * get_audio_player_info_from_filesystem(uint32_t index, bool mode)
{
    printf("%s,index:%d\n", __FUNCTION__, index);
    ui_file_t * pfile = NULL;
    uint32_t ret_len = 0;
    int ret = LV_FS_RES_OK;

    char path[6];
    strcpy(path, AUDIO_ROOT_PATH);
    audio_info_t audio_error_info;
    memset(&audio_error_info,0,sizeof(audio_info_t));
    strcpy(audio_error_info.path,AUDIO_ROOT_PATH);
    strcpy(audio_error_info.filename,"read song error!");
#ifndef BUILD_IN_PC_SIMULATOR
    lv_fs_dir_t dir;
    memset(&dir, 0, sizeof(dir));
    ret = lv_fs_dir_open(&dir, path);
    printf("ret is %d in %s\n",ret,__FUNCTION__);
    if(ret == LV_FS_RES_OK) {
        printf("opendir OK handle:0x%x in get_audio_player_info_from_filesystem\n", *(uint32_t *)dir.dir_d);
        lv_fs_dir_close(&dir);
#else
    if(1) {
#endif

#if USE_LV_SD_FS
        uint32_t total = 0;
        uint32_t free_size = 0;
        ret = lv_fs_free_space(AUDIO_SD_FS_LETTER, &total, &free_size);
        printf("ret is %d in lv_fs_free_space\n",ret);
        printf("audio_player_check_space total = %dKB, free = %dKB\n", total, free_size);
        if(free_size < AUDIO_LESS_SPACE_NUM) {
            printf("audio_player_check_space is not enough\n");
            audio_player_sd_is_full();
            memcpy(&audio_info_global,&audio_error_info,sizeof(audio_info_t));
            return &audio_info_global;
        }
#endif
        pfile = lv_mem_alloc(sizeof(ui_file_t));
        memset(pfile,0,sizeof(ui_file_t));
        ret = lv_fs_open(pfile,ADUIO_FILE_SYSTEM_PATH,LV_FS_MODE_RD);
        printf("ret is %d in lv_fs_open\n",ret);
        if(LV_FS_RES_OK != ret) {
            printf("D:\\audio_list.nv is not exist!\n");
            play_cnt = 0;
            if(true == mode)
            {
                ret = lv_fs_close(pfile);
                printf("ret is %d in lv_fs_close\n",ret);
                printf("create D:\\audio_list.nv\n");
                memset(pfile,0,sizeof(ui_file_t));
                ret = lv_fs_open(pfile,ADUIO_FILE_SYSTEM_PATH,LV_FS_MODE_WR);
                printf("ret is %d in lv_fs_open\n",ret);
                if(LV_FS_RES_OK != ret)
                {
                    printf("D:\\audio_list.nv create fail!\n");
                    lv_mem_free(pfile);
                    memcpy(&audio_info_global,&audio_error_info,sizeof(audio_info_t));
                    return &audio_info_global;
                }
                ret = lv_fs_close(pfile);
                printf("ret is %d in lv_fs_close\n",ret);
                memset(pfile,0,sizeof(ui_file_t));
                ret = lv_fs_open(pfile,ADUIO_FILE_SYSTEM_PATH,LV_FS_MODE_RD | LV_FS_MODE_APPEND);
                printf("ret is %d in lv_fs_open\n",ret);
                if(LV_FS_RES_OK != ret) {
                    printf("D:\\audio_list.nv second open failed!\n");
                    LV_ASSERT_MEM(pfile->file_d);
                    lv_mem_free(pfile);
                    memcpy(&audio_info_global,&audio_error_info,sizeof(audio_info_t));
                    return &audio_info_global;
                }
                ret = lv_fs_seek(pfile, 0);
                printf("ret is %d in lv_fs_seek\n",ret);
                ret = lv_fs_write(pfile, &play_cnt, sizeof(uint32_t) ,&ret_len);
                printf("ret is %d in lv_fs_write\n",ret);
                if(0 == ret_len)
                {
                    printf("write play_cnt in sdfs is wrong!\n");
                    lv_fs_close(pfile);
                    lv_mem_free(pfile);
                    memcpy(&audio_info_global,&audio_error_info,sizeof(audio_info_t));
                    return &audio_info_global;
                }
                get_audio_player_info(path,pfile);
                ret = lv_fs_seek(pfile, 0);
                printf("ret is %d in lv_fs_seek\n",ret);
                ret = lv_fs_read(pfile, &play_cnt, sizeof(uint32_t) ,&ret_len);
                printf("ret is %d in lv_fs_read\n",ret);
                if(sizeof(uint32_t) != ret_len)
                {
                    printf("read play_cnt in sdfs is wrong!\n");
                    lv_fs_close(pfile);
                    lv_mem_free(pfile);
                    memcpy(&audio_info_global,&audio_error_info,sizeof(audio_info_t));
                    return &audio_info_global;
                }
                ret = lv_fs_seek(pfile, index * AUDIO_PATH_MAX * 2 + sizeof(uint32_t));
                printf("ret is %d in lv_fs_seek\n",ret);
                memset(&audio_info_global,0,sizeof(audio_info_t));                // read data
                printf("audio_info_global has been memset\n");
                ret = lv_fs_read(pfile, audio_info_global.path, AUDIO_PATH_MAX ,&ret_len);
                printf("ret is %d in lv_fs_read\n",ret);
                if(AUDIO_PATH_MAX != ret_len)
                {
                    printf("read path in sdfs is wrong!\n");
                    lv_fs_close(pfile);
                    lv_mem_free(pfile);
                    memcpy(&audio_info_global,&audio_error_info,sizeof(audio_info_t));
                    return &audio_info_global;
                }
                ret = lv_fs_seek(pfile, index * AUDIO_PATH_MAX * 2 + AUDIO_PATH_MAX + sizeof(uint32_t));
                printf("ret is %d in lv_fs_seek\n",ret);
                ret = lv_fs_read(pfile, audio_info_global.filename, AUDIO_PATH_MAX ,&ret_len);
                printf("ret is %d in lv_fs_read\n",ret);
                ret = lv_fs_close(pfile);
                printf("ret is %d in lv_fs_close\n",ret);
                lv_mem_free(pfile);
                if(AUDIO_PATH_MAX != ret_len) {
                    printf("read filename in sdfs is wrong!\n");
                    memcpy(&audio_info_global,&audio_error_info,sizeof(audio_info_t));
                    return &audio_info_global;
                }
            } else {
                lv_mem_free(pfile);
            }
            //return NULL;
        } else {
            printf("D:\\audio_list.nv is exist!\n");
            if(true == mode)
            {
                ret = lv_fs_close(pfile);
                printf("ret is %d in lv_fs_close\n",ret);
                printf("create D:\\audio_list.nv\n");
                memset(pfile,0,sizeof(ui_file_t));
                ret = lv_fs_open(pfile,ADUIO_FILE_SYSTEM_PATH,LV_FS_MODE_WR);
                printf("ret is %d in lv_fs_open\n",ret);
                if(LV_FS_RES_OK != ret)
                {
                    printf("D:\\audio_list.nv create fail!\n");
                    lv_mem_free(pfile);
                    memcpy(&audio_info_global,&audio_error_info,sizeof(audio_info_t));
                    return &audio_info_global;
                }
                ret = lv_fs_close(pfile);
                printf("ret is %d in lv_fs_close\n",ret);
                memset(pfile,0,sizeof(ui_file_t));
                ret = lv_fs_open(pfile,ADUIO_FILE_SYSTEM_PATH,LV_FS_MODE_RD | LV_FS_MODE_APPEND);
                printf("ret is %d in lv_fs_open\n",ret);
                if(LV_FS_RES_OK != ret) {
                    printf("D:\\audio_list.nv second open failed!\n");
                    LV_ASSERT_MEM(pfile->file_d);
                    lv_mem_free(pfile);
                    memcpy(&audio_info_global,&audio_error_info,sizeof(audio_info_t));
                    return &audio_info_global;
                }
                play_cnt = 0;
                ret = lv_fs_seek(pfile, 0);
                printf("ret is %d in lv_fs_seek\n",ret);
                ret = lv_fs_write(pfile, &play_cnt, sizeof(uint32_t) ,&ret_len);
                printf("ret is %d in lv_fs_write\n",ret);
                if(0 == ret_len)
                {
                    printf("write play_cnt in sdfs is wrong!\n");
                    lv_fs_close(pfile);
                    lv_mem_free(pfile);
                    memcpy(&audio_info_global,&audio_error_info,sizeof(audio_info_t));
                    return &audio_info_global;
                }
                get_audio_player_info(path, pfile);
            }
            ret = lv_fs_seek(pfile, 0);
            printf("ret is %d in lv_fs_seek\n",ret);
            ret = lv_fs_read(pfile, &play_cnt, sizeof(uint32_t) ,&ret_len);
            printf("ret is %d in lv_fs_read\n",ret);
            if(sizeof(uint32_t) != ret_len)
            {
                printf("read play_cnt in sdfs is wrong!\n");
                lv_fs_close(pfile);
                lv_mem_free(pfile);
                memcpy(&audio_info_global,&audio_error_info,sizeof(audio_info_t));
                return &audio_info_global;
            }
            ret = lv_fs_seek(pfile, index * AUDIO_PATH_MAX * 2 + sizeof(uint32_t));
            printf("ret is %d in lv_fs_seek\n",ret);
            memset(&audio_info_global,0,sizeof(audio_info_t));                // read data
            printf("audio_info_global has been memset\n");
            ret = lv_fs_read(pfile, audio_info_global.path, AUDIO_PATH_MAX ,&ret_len);
            printf("ret is %d in lv_fs_read\n",ret);
            if(AUDIO_PATH_MAX != ret_len)
            {
                printf("read path in sdfs is wrong!\n");
                lv_fs_close(pfile);
                lv_mem_free(pfile);
                memcpy(&audio_info_global,&audio_error_info,sizeof(audio_info_t));
                return &audio_info_global;
            }
            ret = lv_fs_seek(pfile, index * AUDIO_PATH_MAX * 2 + AUDIO_PATH_MAX + sizeof(uint32_t));
            printf("ret is %d in lv_fs_seek\n",ret);
            ret = lv_fs_read(pfile, audio_info_global.filename, AUDIO_PATH_MAX ,&ret_len);
            printf("ret is %d in lv_fs_read\n",ret);
            ret = lv_fs_close(pfile);
            printf("ret is %d in lv_fs_close\n",ret);
            lv_mem_free(pfile);
            if(AUDIO_PATH_MAX != ret_len) {
                printf("read filename in sdfs is wrong!\n");
                memcpy(&audio_info_global,&audio_error_info,sizeof(audio_info_t));
                return &audio_info_global;
            }
        }
    } else {
        printf("opendir %s failed in get_audio_player_info_from_filesystem!\n", path);
        play_cnt = 0;
        memcpy(&audio_info_global,&audio_error_info,sizeof(audio_info_t));
        return &audio_info_global;
    }
    printf("play_cnt is %d\n",play_cnt);
    printf("index is %d,audio_info_global.path is %s,audio_info_global.filename is %s\n",index,audio_info_global.path,audio_info_global.filename);
    return &audio_info_global;
}

static void get_audio_player_info(char * path, ui_file_t * pfile)
{
    printf("%s,path:%s\n", __FUNCTION__, path);
#ifndef BUILD_IN_PC_SIMULATOR
    lv_fs_dir_t dir;
    char * filename = (char *)lv_mem_alloc(AUDIO_PATH_MAX);
    char * newpath = NULL;
    char *fullpathname = NULL;
    const char * extname = NULL;
    int ret = LV_FS_RES_OK;
    uint32_t     endtimetemp = 0;

    memset(&dir, 0, sizeof(dir));
    ret = lv_fs_dir_open(&dir, path);
    if(ret == LV_FS_RES_OK) {
        printf("opendir OK handle:0x%x\n", *(uint32_t *)dir.dir_d);
        while((ret & 0x0F) == LV_FS_RES_OK) {
            memset(filename, 0, AUDIO_PATH_MAX);
            ret = lv_fs_dir_read(&dir, filename);
            if((ret & 0x0F) == LV_FS_RES_OK) {
                printf("-->read file name:%s attr:%x\n", filename, (ret & 0xF0));
                switch((ret & 0xF0)) {
                    case AUDIO_FILE_ARC:
                        extname = lv_fs_get_ext(filename);
                        printf("-->ext name:%s\n", extname);
                        if((false == strcmp(AUDIO_EXT_NAME, extname)) || (false == strcmp(AUDIO_EXT_NAME_SUB, extname))) {
                            printf("cmp ok-->path:%s,filename%s\n", path, filename);
                            fullpathname = (char*)lv_mem_alloc(strlen(path) + strlen(filename) + 5);
                            memset(fullpathname,0,strlen(path) + strlen(filename) + 5);
                            memcpy(fullpathname,path,(strlen(path) - 1)); //without *
                            strcat(fullpathname,filename);
                            endtimetemp = 0;
                            Hal_File_Get_AudioDurationTime(fullpathname, &endtimetemp);
                            lv_mem_free(fullpathname);
                            printf("the full time is %d in %s\n",endtimetemp,__FUNCTION__);
                            if(endtimetemp)
                            {
                                store_audio_player_info_to_global(path,filename,pfile);
                            }
                            else
                            {
                                printf("the music can not be recorded in the audio list: %s\n",__FUNCTION__);
                            }
                        }
                        break;
                    case AUDIO_FILE_DIR:
                        newpath = (char *)lv_mem_alloc(strlen(path) + strlen(filename) + 5);
                        memset(newpath, 0, strlen(path) + strlen(filename) + 5);
                        memcpy(newpath, path, (strlen(path) - 1)); //without *
                        strcat(newpath, filename);
                        strcat(newpath, "\\*");
                        get_audio_player_info(newpath, pfile);
                        lv_mem_free(newpath);
                        break;
                    case AUDIO_FILE_HID:
                    default:
                        break;
                }
            }
        }
        lv_fs_dir_close(&dir);
    } else {
        printf("opendir %s failed!!!\n", path);
    }
    lv_mem_free(filename);
#else
    store_audio_player_info_to_global(path, "11112222333344445555.mp3", pfile);
    store_audio_player_info_to_global(path, "2.mp3", pfile);
    store_audio_player_info_to_global(path, "3.mp3", pfile);
    store_audio_player_info_to_global(path, "4.mp3", pfile);
    store_audio_player_info_to_global(path, "5.mp3", pfile);
    store_audio_player_info_to_global(path, "6.mp3", pfile);
#endif
}

static void audio_player_get_endtime(void)
{
    char * path = NULL;
    audio_info_t  * audio_info = get_audio_player_info_from_filesystem(current_index, false);

    printf("audio_info is %lx,current_index is %d in audio_player_get_endtime\n", (unsigned long)audio_info, current_index);
    if(0 != strcmp(audio_info->filename,"read song error!")) {
        path = (char *)lv_mem_alloc(strlen(audio_info->path) + strlen(audio_info->filename) + 1);
        memset(path, 0, strlen(audio_info->path) + strlen(audio_info->filename) + 1);
        strcpy(path, audio_info->path);
        strcat(path, audio_info->filename);
#ifndef BUILD_IN_PC_SIMULATOR
        Hal_File_Get_AudioDurationTime(path, &endtime);
#endif
        printf("endtime is %u in audio_player_get_endtime\n", endtime);
        lv_mem_free(path);
    }
}

static bool audio_player_display_time(void)
{
    uint32_t mseconds;
    lv_audio_player_ext_t * img_ext = audio_player_get_ext();

#ifndef BUILD_IN_PC_SIMULATOR
    printf("endtime is %u in audio_player_display_time\n", endtime);
#else
    endtime = 100000;
#endif
    if(AUDIO_PLAYER_STATE_RUN == ap_state) {
        Hal_Audio_Get_PlayInformation(&current_playinfo);
    }

    mseconds = current_playinfo.curPlayTimeMs;
    printf("mseconds is %u in audio_player_display_time\n", mseconds);
    uint8_t sec  = (mseconds / 1000) % 60;         /*[0,59]*/
    uint8_t min  = (mseconds / 1000 / 60) % 60;    /*[0,59]*/

    uint8_t sec_end  = (endtime / 1000) % 60;         /*[0,59]*/
    uint8_t min_end  = (endtime / 1000 / 60) % 60;    /*[0,59]*/

    bool flag;

    char str[14];
    char str1[6];
    char str2[6];

    if(mseconds >= endtime) {
        flag = true;
        snprintf(str1, 6, "%02d:%02d", min_end, sec_end);
        snprintf(str2, 6, "%02d:%02d", min_end, sec_end);
    } else {
        flag = false;
        snprintf(str1, 6, "%02d:%02d", min, sec);
        snprintf(str2, 6, "%02d:%02d", min_end, sec_end);
    }

    if(NULL == img_ext) {
        printf("img_ext is %lx in audio_player_display_time\n", (unsigned long)img_ext);
        return (flag);
    }

    if(lv_obj_get_base_dir(lv_obj_get_parent(img_ext->label_time_s)) == LV_BIDI_DIR_RTL)
        snprintf(str, 14, "%s/%s",str2, str1);
    else
        snprintf(str, 14, "%s/%s",str1, str2);

    lv_label_set_text(img_ext->label_time_s, str);

    if(endtime > 0) {
        lv_slider_set_range(img_ext->slider, 0, endtime / 1000);

        if(false == lv_slider_is_dragged(img_ext->slider))
            lv_slider_set_value(img_ext->slider, mseconds / 1000, LV_ANIM_OFF);

        lv_obj_set_click(img_ext->slider, true);
    }

    return(flag);
}

static void audio_player_task(lv_task_t * task)
{
    if(AUDIO_PLAYER_STATE_RUN != ap_state) {
        return;
    }

    audio_player_display_time();
}

static void audio_player_img_start_signal(lv_obj_t * img, lv_event_t e)
{
    if(e == LV_EVENT_RELEASED) {
        if(AUDIO_PLAYER_STATE_IDLE == ap_state) {
            printf("%s,AUDIO_PLAYER_STATE_IDLE!\n", __FUNCTION__);
            return;
        }

#if USE_LV_SD_FS
        if(true != sd_remount_flg) {
#if USE_LV_WATCH_SD_AUTO_MOUNT != 0
            if(true == setting_sd_mount_capture_check(SETTING_CAPTURE_SD_APP_AUDIO_PLAYER)) {
#else
            if(SD_MOUNTED_AS_U_STORAGE == HAL_sd_user_set(true)) {
#endif
                printf("%s,SD card is mounted as U storage!\n", __FUNCTION__);
                return;
            } else {
                sd_remount_flg = true;
            }
        }
#endif
        audio_info_t * audio_info = get_audio_player_info_from_filesystem(current_index, false);
        if(0 != strcmp(audio_info->filename,"read song error!")) {
            char * path = (char *)lv_mem_alloc(strlen(audio_info->path) + strlen(audio_info->filename) + 1);
            memset(path, 0, strlen(audio_info->path) + strlen(audio_info->filename) + 1);
            strcpy(path, audio_info->path);
            strcat(path, audio_info->filename);
            audio_player_get_endtime();
            Hal_Audio_Manage_Start_Req(AUDIO_CTRL_PRIORITY_6, audio_player_audio_ctrl_callback);
            Hal_File_Play_Onetime(path,
                                query_current_volume(),
                                audio_player_music_over_cb,
                                NULL,
                                current_playinfo.PlayProgress);
            lv_mem_free(path);

            printf("%s,SD card is captured by user!\n", __FUNCTION__);
            ap_state = AUDIO_PLAYER_STATE_RUN;
            printf("%s\n", __FUNCTION__);
            watch_set_sleep_lcd_only(true);
            audio_player_auto_user_interface_update();
            if(audio_player_get_ext()) {
                if(NULL == audio_player_task_p) {
                    audio_player_task_p = lv_task_create(audio_player_task, 1000, LV_TASK_PRIO_MID, NULL);
                }
            }
        }
    }
    return;
} /* audio_player_img_start_signal */

static void audio_player_img_pause_signal(lv_obj_t * img, lv_event_t e)
{
    if(e == LV_EVENT_RELEASED) {
        ap_state = AUDIO_PLAYER_STATE_PAUSE;

        audio_player_auto_user_interface_update();
        Hal_Audio_Get_PlayInformation(&current_playinfo);
        printf("%s:curPlayTimeMs is %d,PlayProgress is %d\n", __FUNCTION__, current_playinfo.curPlayTimeMs, current_playinfo.PlayProgress);
        Hal_File_Play_End();
        Hal_Audio_Manage_End_Ind(AUDIO_CTRL_PRIORITY_6);
        if(NULL != audio_player_task_p) {
            lv_task_del(audio_player_task_p);
            audio_player_task_p = NULL;
        }
        watch_set_sleep_lcd_only(false);

#if USE_LV_SD_FS
        if(NULL == lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER)) {
            if(true == sd_remount_flg) {
                printf("%s,SD card is released by user!\n", __FUNCTION__);
#if USE_LV_WATCH_SD_AUTO_MOUNT != 0
                setting_sd_mount_app_release(SETTING_CAPTURE_SD_APP_AUDIO_PLAYER);
#else
                if(HAL_sd_user_get()) {
                    HAL_sd_user_set(false);
                }
#endif
                sd_remount_flg = false;
            }
        }
#endif
    }
    return;
} /* audio_player_img_pause_signal */

static void audio_player_img_prev_signal(lv_obj_t * img, lv_event_t e)
{
    if((AUDIO_PLAYER_STATE_IDLE == ap_state) || (0 == play_cnt)) {
        return;
    }

    if(e == LV_EVENT_RELEASED) {
        if(NV_AUDIO_SHUFFLE_PLAY == get_audio_player_mode()) {
            current_index = rand() % (play_cnt);
        } else {
            if(0 == current_index) {
                current_index = play_cnt - 1;
            } else {
                current_index--;
            }
        }

        audio_info_t * audio_info = get_audio_player_info_from_filesystem(current_index, false);
        char * path = (char *)lv_mem_alloc(strlen(audio_info->path) + strlen(audio_info->filename) + 1);
        memset(path, 0, strlen(audio_info->path) + strlen(audio_info->filename) + 1);
        strcpy(path, audio_info->path);
        strcat(path, audio_info->filename);
        audio_player_get_endtime();
        Hal_File_Play_End();
        memset(&current_playinfo, 0, sizeof(hal_play_info_t));
        if(AUDIO_PLAYER_STATE_RUN == ap_state) {
            Hal_File_Play_Onetime(path,
                                  query_current_volume(),
                                  audio_player_music_over_cb,
                                  NULL,
                                  0);
        }

        printf("%s:ringvolume is %s,current_index is %d\n", __FUNCTION__, path, current_index);
        lv_mem_free(path);
        audio_player_display_time();
        audio_player_auto_user_interface_update();
    }

    return;
} /* audio_player_img_prev_signal */

static void audio_player_img_next_signal(lv_obj_t * img, lv_event_t e)
{
    if((AUDIO_PLAYER_STATE_IDLE == ap_state) || (0 == play_cnt)) {
        return;
    }

    if(e == LV_EVENT_RELEASED) {
        if(NV_AUDIO_SHUFFLE_PLAY == get_audio_player_mode()) {
            current_index = rand() % (play_cnt);
        } else {
            if(play_cnt == (current_index + 1)) {
                current_index = 0;
            } else {
                current_index++;
            }
        }

        audio_info_t * audio_info = get_audio_player_info_from_filesystem(current_index, false);
        char * path = (char *)lv_mem_alloc(strlen(audio_info->path) + strlen(audio_info->filename) + 1);
        memset(path, 0, strlen(audio_info->path) + strlen(audio_info->filename) + 1);
        strcpy(path, audio_info->path);
        strcat(path, audio_info->filename);
        audio_player_get_endtime();
        Hal_File_Play_End();
        memset(&current_playinfo, 0, sizeof(hal_play_info_t));
        if(AUDIO_PLAYER_STATE_RUN == ap_state) {
            Hal_File_Play_Onetime(path,
                                  query_current_volume(),
                                  audio_player_music_over_cb,
                                  NULL,
                                  0);
        }

        printf("%s:ringvolume is %s,current_index is %d\n", __FUNCTION__, path, current_index);
        lv_mem_free(path);
        audio_player_display_time();
        audio_player_auto_user_interface_update();
    }

    return;
} /* audio_player_img_next_signal */

#if USE_LV_SD_FS
static void audio_player_sd_mounted_as_storage(lv_obj_t * obj)
{
    lv_obj_t * label = lv_label_create(obj, NULL);
    if(1 == lv_lang_act()) {
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font30);
    } else {
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20);
    }

    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_SD_MOUNTED_AS_U_STORAGE);
    lv_obj_align(label, obj, LV_ALIGN_CENTER, 0, 0);

    return;
}

static void audio_player_sd_is_full(void)
{
    printf("%s\n", __FUNCTION__);
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_ALL_SONGS);
    if(activity_obj == NULL) return;

    lv_obj_t * watch_obj;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    lv_obj_t * label = lv_label_create(watch_obj, NULL);
    if(1 == lv_lang_act()) {
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font30);
    } else {
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20);
    }

    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_SD_IS_FULL);
    lv_obj_align(label, watch_obj, LV_ALIGN_CENTER, 0, 0);

    return;
}
#endif

#if USE_LV_WATCH_SD_AUTO_MOUNT != 0
void audio_player_for_mount_destory(void)
{
    printf("%s\n", __FUNCTION__);
    audio_player_img_pause_signal(NULL, LV_EVENT_RELEASED);
    audio_player_prepare_destory(NULL);
}
#endif

#if USE_LV_WATCH_SD_AUTO_MOUNT != 0
#if USE_LV_SD_FS
static void audio_player_inquire_sd_task_cb(lv_task_t * task)
{
    if(SD_REMOUNT_OK > HAL_sd_user_get()) return;

    if(par_tab[0]) {
        lv_obj_clean(par_tab[0]);
        audio_player_create_content(par_tab[0]);
    }

    inquire_sd_task_p = NULL;
    lv_task_del(task);
}

void audio_player_for_sd_mount(void)
{
    if(inquire_sd_task_p) {
        lv_task_del(inquire_sd_task_p);
        inquire_sd_task_p = NULL;
    }

    if(NULL == par_tab[0]) return;

    lv_obj_clean(par_tab[0]);
    audio_player_sd_mounted_as_storage(par_tab[0]);

    if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_ALL_SONGS)) {
        lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_ALL_SONGS));
    }
    if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_OPT)) {
        lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_OPT));
    }
    if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_SETTINGS)) {
        lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_SETTINGS));
    }
}

void audio_player_for_sd_unmount(void)
{
    if(inquire_sd_task_p) return;
    if(NULL == par_tab[0]) return;

    inquire_sd_task_p = lv_task_create(audio_player_inquire_sd_task_cb, 200,
                                       LV_TASK_PRIO_MID, NULL);
}
#endif
#endif

void audio_player_lowbattery_process(void)
{
    printf("%s\r\n",__FUNCTION__);
    if(AUDIO_PLAYER_STATE_RUN == ap_state)
    {
        audio_player_img_pause_signal(NULL, LV_EVENT_RELEASED);
    }
    if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_ALL_SONGS)) {
        lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_ALL_SONGS));
    }
    if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_OPT)) {
        lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_OPT));
    }
    if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_SETTINGS)) {
        lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER_SETTINGS));
    }
    if(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER))
    {
        audio_player_prepare_destory(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER));
        lv_obj_del(lv_watch_get_activity_obj(ACT_ID_AUDIO_PLAYER));
    }
}
#endif
